package code._16reflect._04field;

import untils.FileUtils;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;

public class Demo {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        Class<?> personCls = Class.forName("code._16reflect.bean.Person");
        System.out.println("---获取所有的public成员变量-----");
        Field[] fields = personCls.getFields();
        for (Field field : fields) {
            System.out.println("field = " + field);
        }

        System.out.println("----获取所有的成员变量-------");
        Field[] declaredFields = personCls.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            System.out.println("declaredField = " + declaredField);
        }

        System.out.println("---获取指定的public成员变量------");
        Field nameField = personCls.getField("name");
        System.out.println("nameField = " + nameField);

        System.out.println("---获取指定的成员变量------");
        Field ageFiled = personCls.getDeclaredField("age");
        System.out.println("ageFiled = " + ageFiled);

        //实例化一个person对象
        Constructor<?> declaredConstructor = personCls.getDeclaredConstructor();
        Object o = declaredConstructor.newInstance();
        System.out.println("o = " + o);
        nameField.set(o,"zs");
        System.out.println("o = " + o);



    }
}
